//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.basic.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import net.mingsoft.base.biz.impl.BaseBizImpl;
import net.mingsoft.base.dao.IBaseDao;
import net.mingsoft.base.entity.BaseEntity;
import net.mingsoft.basic.bean.RoleBean;
import net.mingsoft.basic.biz.IManagerBiz;
import net.mingsoft.basic.biz.IModelBiz;
import net.mingsoft.basic.biz.IRoleBiz;
import net.mingsoft.basic.biz.IRoleModelBiz;
import net.mingsoft.basic.dao.IRoleDao;
import net.mingsoft.basic.entity.ManagerEntity;
import net.mingsoft.basic.entity.RoleEntity;
import net.mingsoft.basic.entity.RoleModelEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("roleBiz")
@Transactional
public class RoleBizImpl extends BaseBizImpl<IRoleDao, RoleEntity> implements IRoleBiz {
    @Autowired
    private IRoleDao roleDao;
    @Autowired
    private IRoleModelBiz roleModelBiz;
    @Autowired
    private IModelBiz modelBiz;
    @Autowired
    private IManagerBiz managerBiz;

    public RoleBizImpl() {
    }

    public IBaseDao getDao() {
        return this.roleDao;
    }

    public boolean saveOrUpdateRole(RoleBean role) {
        RoleBean oldRole = new RoleBean();
        oldRole.setRoleName(role.getRoleName());
        oldRole = (RoleBean)this.roleDao.getByEntity(oldRole);
        if (StringUtils.isNotEmpty(role.getId())) {
            if (oldRole != null && !oldRole.getId().equals(role.getId())) {
                return false;
            }

            this.roleDao.updateEntity(role);
        } else {
            if (oldRole != null) {
                return false;
            }

            this.save(role);
            this.roleDao.updateCache();
        }

        List<RoleModelEntity> roleModelList = new ArrayList();
        if (!StringUtils.isEmpty(role.getIds())) {
            String[] var4 = role.getIds().split(",");
            int var5 = var4.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                String id = var4[var6];
                RoleModelEntity roleModel = new RoleModelEntity();
                roleModel.setRoleId(Integer.parseInt(role.getId()));
                roleModel.setModelId(Integer.parseInt(id));
                roleModelList.add(roleModel);
            }

            this.roleModelBiz.deleteEntity(Integer.parseInt(role.getId()));
            this.modelBiz.updateCache();
            this.roleModelBiz.saveBatch(roleModelList, roleModelList.size());
        } else {
            this.roleModelBiz.deleteEntity(Integer.parseInt(role.getId()));
        }

        return true;
    }

    public boolean deleteRoleByRoles(List<RoleEntity> roles, ManagerEntity managerSession) {
        List<String> roleIdList = (List)roles.stream().map(BaseEntity::getId).collect(Collectors.toList());
        int before = roleIdList.size();
        List<String> managerRoleIds = (List)Arrays.stream(managerSession.getRoleIds().split(",")).collect(Collectors.toList());
        LambdaQueryWrapper<RoleEntity> wrapper = new LambdaQueryWrapper();
        wrapper.eq(RoleEntity::getNotDel, 1);
        List<RoleEntity> notDelRoles = super.list(wrapper);
        if (notDelRoles != null && notDelRoles.size() > 0) {
            List<String> notDelRoleIds = (List)notDelRoles.stream().map(BaseEntity::getId).collect(Collectors.toList());
            roleIdList.removeAll(notDelRoleIds);
        }

        roleIdList.removeAll(managerRoleIds);
        int after = roleIdList.size();
        int[] ids = roleIdList.stream().mapToInt(Integer::valueOf).toArray();
        if (ids.length > 0) {
            super.delete(ids);
            this.roleModelBiz.deleteByRoleIds(ids);
            super.updateCache();
        }

        return before == after;
    }
}
